LearningPerlLite
=Introduction= * PERL Practical Extraction and Report Language / Pathologically Eclectic Rubbish Lister * Get Perl http://www.perl.org/get.html ** http://www.strawberryperl.com ** http://www.activestate.com Best Books * The Camel book * Learning Perl * The Perl Cookbook what-are-good-books-for-learning-perl Special Variables * $_ * $#rocks * $_0, $_1 or @_ (my($m, $n) = @_; equals $m = $_0; $n = $_1;) =Singular Data= Number There are no integer values internal to Perl, an integer constant in the program is treated as the equivalent floating-point value. 0377 # 377 octal 0xff # FF hex, also 255 0b11111111 # also 255 decimal Numeric Operators: + - * / String Perl does not interpret the \n within a single-quoted string as a newline, but as the two characters backslash and n. 'abc@email.com' eq "abc\@email.com". 'hello\n' # hello followed by backslash followed by n 'hello there' # hello, newline, there (11 characters total) "hello world\n" # hello world, and a newline Double quote escapes \n Newline \r Return \t Tab \f Formfeed \b Backspace \a Bell \e Escape (ASCII escape character) \007 Any octal ASCII value (here, 007 = bell) \x7f Any hex ASCII value (here, 7f = delete) \x{2744} Any hex Unicode code point (here, U+2744 = snowflake) \cC A “control” character (here, Ctrl-C) \\ Backslash \“ Double quote \l Lowercase next letter \L Lowercase all following letters until \E \u Uppercase next letter \U Uppercase all following letters until \E \Q Quote nonword characters by adding a backslash until \E \E End \L, \U, or \Q String Operators "hello" . "world" # same as "helloworld" "hello" . ' ' . "world" # same as 'hello world' 'hello world' . "\n" # same as "hello world\n" "fred" x 3 # is "fredfredfred" "barney" x (4+1) # is "barney" x 5 Null * undef if (defined(...)){ ... } Assignment $fred = 17; # give $fred the value of 17 $barney = 'hello'; # give $barney the five-character string 'hello' $fred = $fred + 5; # without the binary assignment operator $fred += 5; # with the binary assignment operator $barney = $barney * 3; $barney *= 3; $str = $str . " "; # append a space to $str $str .= " "; # same thing with assignment operator =Plural Data= List (1, 2, 3) # list of three values 1, 2, and 3 (1, 2, 3,) # the same three values (the trailing comma is ignored) ("fred", 4.5) # two values, "fred" and 4.5 ( ) # empty list - zero elements (1..100) # list of 100 integers Array $rocks[ $#rocks ] = 'hard rock'; # the last rock @quarry = (@rocks, "crushed rock", @tiny, $dino); Array Operators * pop * push * shift * unshift * splice =Loop= foreach @rocks = qw/ bedrock slate lava /; foreach $rock (@rocks) { $rock = "\t$rock"; # put a tab in front of each element of @rocks $rock .= "\n"; # put a newline on the end of each } print "The rocks are:\n", @rocks; # Each one is indented, on its own line while $count = 0; while ($count < 10) { $count += 2; print "count is now $count\n"; # Gives values 2 4 6 8 10 } =Subroutines= sub max { # Compare this to &larger_of_fred_or_barney if ($_0 > $_1) { $_0; # return $_0 } else { $_1; } } ... $n = &max(10, 15); # This sub call has two parameters =Input and Output= open CONFIG, '<', 'dino'; open BEDROCK, '>', $file_name; open LOG, '>>', &logfile_name();